package grib2handler.grib;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import grib2handler.grib.ftp.FTPLinkPool;
import grib2handler.util.FileUtil;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import grib2handler.grib.netcdf.GribPass;
import grib2handler.util.PropertiesUtil;
import grib2handler.util.TimeUtil;
/**
 * grib下载定时任务
 * @author charlie
 *
 */
public class GribPoolDownTask  implements Job  {
	private final static Logger log = Logger.getLogger(GribPoolDownTask.class);
	private static ThreadPoolExecutor pool =  null;
	//最新运行时间，判断ftp阻断
	private static long lastRunTime = System.currentTimeMillis();
	public final static String[] ybArray = new String[]{"06", "09", "12", "15", "18", "21", "24", "27", "30" , "33", "36"};
	
	private static String gribOutFile = PropertiesUtil.getConfig().getString("gribOutFile") + "/";
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		log.info("grib定时任务开始");
		// 计算线程是否阻断 堆积任务书多余5，10分钟没有新的任务执行
		if(pool != null && pool.getQueue().size() > 5 &&
				(System.currentTimeMillis() - lastRunTime) > 10 * 60 * 1000) {
			log.warn("线程阻塞,尝试关闭重启线程池");
			//pool关闭，重新创建
			try {
				pool.shutdownNow();
				pool = null;
			} catch (Exception e) {
				log.error("线程阻塞，线程池关闭异常" + e.getMessage());
			} 
			try {
				pool = new ThreadPoolExecutor(4, 20, 10, TimeUnit.SECONDS,
						new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.AbortPolicy());
			} catch (Exception e) {
				log.error("线程阻塞，线程池创建异常" + e.getMessage());
			}
			
			//加载最新数据
			loadLastData();
		}
		//定时清除缓存数据
		GribPass.delOldTemp();
		List<File> filesMicap = FileUtil.readPathFiles(PropertiesUtil.getConfig().getString("micapsexport", "micapsexport"), null);
		Collections.sort(filesMicap);
		int micapsSize = PropertiesUtil.getConfig().getInt("micapsSize", 80);
		if(filesMicap.size()> micapsSize) {
			for (int i = 0; i < filesMicap.size() - micapsSize; i++) {
				FileUtil.delPath(filesMicap.get(i).getAbsolutePath());
			}
		}
		//计算加载时间
		String time = TimeUtil.getUtcTimeStr("yyyyMMddHH");
		loadData(time);
		log.info("grib定时任务完成");
	}
	
	public static void loadData(String time) {
		log.info("加载数据:" +  time);
		//时次
		String shiCi = time.substring(8, 10);
		//输出下载文件路径
		String outFilePath = gribOutFile + time + "/" +
				PropertiesUtil.getConfig().getString("agency", "us_avn") + "/gfs/";
		//循环加载文件
		//for (String yb : ybArray) {
			//String nameGfs = "gfs.t" + shiCi + "z.wafs_grb45" + "f" + yb;
			String nameGfs = "ldgrib2_"+time+".tar";
			//gfs下载
			FTPLinkPool gfsFtp = new FTPLinkPool(
					PropertiesUtil.getConfig().getString("ftp.dir") + "/" + nameGfs ,
					outFilePath + nameGfs , time, nameGfs);
			//启动线程
			if(pool != null) {
				pool.execute(gfsFtp);
			}
		//}
		//循环加载文件
//		for (String yb : ybArray) {
//			String nameWafs = "WAFS_blended_" + time + "f" + yb;
//			//wafs下载
//			FTPLinkPool wafsFtp = new FTPLinkPool(
//					"/pub/data/nccf/com/gfs/prod/gfs." + time + "/" + nameWafs + ".grib2",
//					outFilePath + nameWafs + ".grib2", time, nameWafs);
//			//启动线程
//			if(pool != null) {
//				pool.execute(wafsFtp);
//			}
//		}
	}
	/**
	 * 初始化最新两次数据
	 */
	private static void loadLastData()  {
		FTPLinkPool ftp = new FTPLinkPool(
				PropertiesUtil.getConfig().getString("ftp.dir"),
				"", "", "");
		List<String> nameList = ftp.getFileTimes();
		if(nameList.size() > 2) {
			loadData(nameList.get(nameList.size() - 2).substring(0, 10));
			loadData(nameList.get(nameList.size() - 1).substring(0, 10));
		} else {
			log.error("远程服务器的最新文件没有获取成功");
		}
	}
	/**
	 * 线程初始化数据
	 */
	public static void loadLastDataThread()  {
		new Thread(new Runnable() {
			@Override
			public void run() {
				loadLastData();
			}
		}).start();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		PropertyConfigurator.configure("conf/log4j.properties");

		FTPLinkPool ftp = new FTPLinkPool(
				"/pub/data/nccf/com/gfs/prod/",
				"", "", "");
		List<String> nameList = ftp.getFileTimes();
		for (String time : nameList) {
			loadData(time);
			Thread.sleep(5*60*1000);
		}
	}

	public static ThreadPoolExecutor getPool() {
		return pool;
	}

	public static void setPool(ThreadPoolExecutor pool) {
		GribPoolDownTask.pool = pool;
	}

	public static long getLastRunTime() {
		return lastRunTime;
	}

	public static void setLastRunTime(long lastRunTime) {
		GribPoolDownTask.lastRunTime = lastRunTime;
	}

	public static String getGribOutFile() {
		return gribOutFile;
	}
	
}

